﻿using GFluentCode.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GFluentCode.Entity
{
    /// <summary>
    /// GFluentTableEntity扩展
    /// </summary>
    public static class GFluentTableEntityTemplate
    {
        /// <summary>
        /// 生成实体对象模板
        /// </summary>        
        /// <param name="namespaceStr">命名空间</param>
        /// <returns></returns>
        public static string ToEntityTemplate(this GFluentTableEntity entity, string namespaceStr)
        {
            var sb = new StringBuilder();

            sb.AppendLine($"using System;")
              .AppendLine($"using GFluentCode.Entity;")
              .AppendLine($"using GFluentCode.Tools;")
              .AppendLine()
              .AppendLine($"namespace {namespaceStr}")
              .AppendLine("{")
              .AppendLine($"    /// <summary>")
              .AppendLine($"    /// {entity.TableName}")
              .AppendLine($"    /// </summary>")
              .AppendLine($"    [Serializable]")
              .AppendLine($"    public class {entity.TableName}Entity:GFluentEntityBase")
              .AppendLine("    {");

            foreach (var tc in entity.Columns)
            {
                sb.AppendLine($"        /// <summary>")
                  .AppendLine($"        /// {(string.IsNullOrWhiteSpace(tc.ColumnRemark) ? tc.ColumnName : tc.ColumnRemark)}")
                  .AppendLine($"        /// </summary>");

                if (tc.ColumnName.StartsWith("Is"))
                    sb.AppendLine($"        public bool {tc.ColumnName} {{get;set;}}");
                else if (tc.ColumnName.StartsWith("En"))
                {
                    sb//.AppendLine($"        [GFluentColumn(\"{tc.ColumnName}\")]")
                      .AppendLine($"        public {tc.ColumnName} {tc.ColumnName} {{get;set;}}")                      
                      .AppendLine($"        /// <summary>")
                      .AppendLine($"        /// {tc.ColumnName}中文名")
                      .AppendLine($"        /// </summary>")
                      .AppendLine($"        [GFluentColumn(EnColumnType.Ignore)]")
                      .AppendLine($"        public string {tc.ColumnName}Name => {tc.ColumnName}.GGetEnumDesc();");
                }
                else
                    sb.AppendLine($"        public {tc.ChangeTypeString()} {tc.ColumnName} {{get;set;}}");
            }

            sb.AppendLine($"    }}")
              .AppendLine("}");

            return sb.ToString();
        }

        /// <summary>
        /// 数据类型转换
        /// </summary>
        /// <param name="tc"></param>
        /// <returns></returns>
        static string ChangeTypeString(this GFluentColumnEntity tc)
        {
            var ct = tc.ColumnType.ToLower();

            if (ct.Contains("nvarchar") || ct.Contains("text"))
                return "string";
            else if (ct.Contains("int") || ct.Contains("integer"))
                return "int";
            else if (ct.Contains("image") || ct.Contains("blob"))
                return "byte[]";
            else if (ct.Contains("datetime"))
                return "DateTime";
            else if (ct.Contains("bigint"))
                return "long";
            else if (ct.Contains("bit"))
                return "bool";
            else if (ct.Contains("float") || ct.Contains("real"))
                return "float";
            else if (ct.Contains("decimal"))
                return "decimal";
            else
                return string.Empty;
        }
    }
}
